<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">高校就业信息管理系统</div>
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
      >
        <el-form-item prop="account">
          <el-input v-model="param.account" placeholder="请输入账号">
            <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="param.password"
            @keyup.enter.native="submitForm()"
            placeholder="请输入密码"
          >
            <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="身份" label-width="20%" prop="roleId">
          <el-radio-group v-model="param.roleId">
            <el-radio :label="3">管理员</el-radio>
            <el-radio :label="2">教师</el-radio>
            <el-radio :label="1">学生</el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>
        <el-link @click="drawer = true">点我查看-版本更新日志</el-link>
        <br />
        <el-link>体验测试：</el-link>
        <br />
        <el-link>
          <table border="3">
            <tr>
              <td>角色</td>
              <td>账号</td>
              <td>密码</td>
            </tr>
            <tr>
              <td>管理员</td>
              <td>admin</td>
              <td>admin</td>
            </tr>
            <tr>
              <td>教师</td>
              <td>6666</td>
              <td>6666</td>
            </tr>
            <tr>
              <td>学生</td>
              <td>1234</td>
              <td>1234</td>
            </tr>
          </table>
        </el-link>
      </el-form>
    </div>
    <!-- 版本更新 抽屉 -->
    <el-drawer :visible.sync="drawer" :with-header="true">
      <el-divider>日志: 当前版本{{ log.version }}</el-divider>
      <el-timeline reverse="true">
        <el-timeline-item v-for="(log, index) in logs" :key="index" :timestamp="log.time">
          {{ log.note }}
        </el-timeline-item>
      </el-timeline>
    </el-drawer>
  </div>
</template>

<script>
import { list } from "../../../public/updateRecord.json";
import { login } from "../../api/login.js";
export default {
  data: function () {
    return {
      param: {
        account: null,
        password: null,
        roleId: 1,
      },
      rules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        roleId: [{ required: true, message: "请选择身份", trigger: "change" }],
      },
      log: list[list.length - 1],
      logs: list,
      // 抽屉开关
      drawer: false,
    };
  },
  methods: {
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          login(this.param)
            .then((result) => {
              if (result.status == "200") {
                this.$message.success("登录成功！");
                // 版本更新通知
                this.$notify({
                  title: "通知",
                  type: "success",
                  dangerouslyUseHTMLString: true,
                  message:
                    "<div><h3>当前版本：V." +
                    this.log.version +
                    "</h3><p>更新内容：" +
                    this.log.note +
                    "</p><p>更新时间：" +
                    this.log.time,
                  duration: 10000,
                });
                localStorage.setItem("token", result.data.token);
                localStorage.setItem("roleId", result.data.roleId);
                console.log(result);
                this.$router.push("/");
              } else {
                this.$message.error("账号或者密码错误!");
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          this.$message.error("请输入完整信息！");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(https://c.wallhere.com/photos/72/87/Razer_Inc_Razer_colorful_building_logo_PC_gaming-1983175.jpg!d);
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.6);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
